package com.ljj.controller;

import com.ljj.util.FileStringUtil;
import com.ljj.util.JsonResult;
import com.ljj.util.PdfConverUtil;
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

@Controller
@RequestMapping("/file")
public class FileController {

    @Value("${file.path}")
    private String outpath;

    private String input = "input/";

    /**
     * word转成pdf
     *
     * @param file
     * @return
     */
    @PostMapping("/WordToPdf")
    @ResponseBody
    public JsonResult WordToPdf(MultipartFile file) {
        String suffixName = "pdf";
        //判断后缀名是否为空字符串或者为空  文件是否上传
        if (file == null || file.isEmpty()) {
            return new JsonResult("word转化pdf失败！请上传文件！");
        }
        //获取文件名称
        String oldFileName = file.getOriginalFilename();
        //获取新文件名称
        String newFileName = FileStringUtil.getNewFileName(oldFileName, suffixName);
        // doc/docx to pdf
        try {
            FileInputStream inputStream = new FileInputStream(input + oldFileName);
            FileOutputStream byteArrayOutputStream = new FileOutputStream(outpath + newFileName);
            PdfConverUtil.wordTopdfByAspose(inputStream, byteArrayOutputStream);
        } catch (FileNotFoundException e) {            e.printStackTrace();
            return new JsonResult("word转化pdf失败！");
        }
        return new JsonResult("word转化pdf成功！");
    }

    /**
     * pdf转成word
     *
     * @param file
     * @param suffixName
     * @return
     */
    @PostMapping("/PdfToWord")
    @ResponseBody
    public JsonResult PdfToWord(MultipartFile file, String suffixName) {
        //获取文件名称
        String oldFileName = file.getOriginalFilename();
        //获取新文件名称
        String newFileName = FileStringUtil.getNewFileName(oldFileName, suffixName);
        //创建一个 PdfDocument 对象
        PdfDocument doc = new PdfDocument();
        try {
            //加载 PDF 文件
            doc.loadFromFile(input + oldFileName);
            //将 PDF 转换为流动形态的Word
            doc.getConvertOptions().setConvertToWordUsingFlow(true);
            if (suffixName.equals("doc")) {
                //将PDF转换为Doc格式文件并保存（推荐使用）
                doc.saveToFile(outpath + newFileName, FileFormat.DOC);
            } else {
                //将PDF转换为Docx格式文件并保存
                doc.saveToFile(outpath + newFileName, FileFormat.DOCX);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return new JsonResult("pdf转化word失败！");
        } finally {
            doc.close();
        }
        return new JsonResult("pdf转化word成功！");
    }

}
